<template>
  <el-dialog v-model="dialogVisible" title="个人中心" width="800px" @open="fetchProfile">
    <div class="profile-body">
      <div class="avatar-uploader-container">
        <el-upload
          class="avatar-uploader"
          action="/upload"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
          name="file"
        >
          <img v-if="profileForm.avatar" :src="profileForm.avatar" class="avatar" />
          <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
        </el-upload>
      </div>
      <el-form :model="profileForm" label-width="80px" class="profile-form">
        <el-form-item label="用户名">
          <el-input v-model="profileForm.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="profileForm.name"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="profileForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="profileForm.email"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="profileForm.address" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="简介">
          <el-input v-model="profileForm.introduction" type="textarea"></el-input>
        </el-form-item>
        <el-form-item label="身份">
          <el-select v-model="profileForm.role" placeholder="请选择身份">
            <el-option label="读者" value="reader"></el-option>
            <el-option label="作者" value="author"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onProfileSubmit">保存</el-button>
          <el-button @click="dialogVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </el-dialog>
</template>

<script setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import request from '@/utils/request'

const dialogVisible = defineModel({ type: Boolean, default: false })
const profileForm = ref({})

const fetchProfile = async () => {
  try {
    const res = await request.get('/reader/profile')
    profileForm.value = res
  } catch (error) {
    ElMessage.error('获取个人信息失败')
  }
}

const onProfileSubmit = async () => {
  try {
    await request.put('/reader/profile', profileForm.value)
    ElMessage.success('个人信息更新成功！')
    dialogVisible.value = false
  } catch (error) {
    ElMessage.error('更新失败')
  }
}

const handleAvatarSuccess = (res) => {
  profileForm.value.avatar = res.url
}

const beforeAvatarUpload = (file) => {
  const isJPG = file.type === 'image/jpeg'
  const isLt2M = file.size / 1024 / 1024 < 2

  if (!isJPG) {
    ElMessage.error('Avatar picture must be JPG format!')
  }
  if (!isLt2M) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
  }
  return isJPG && isLt2M
}
</script>

<style scoped>
.profile-body {
  display: flex;
}
.avatar-uploader-container {
  margin-right: 40px;
  text-align: center;
}
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}
.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}
.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.profile-form {
  flex: 1;
}
</style>